package com.hualude.roadsign.utils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by xu_jiaHui on 2018/3/19.
 */
public class UdpSender {
    private static volatile UdpSender instance;
    private DatagramSocket ds;
    private DatagramPacket dp;

    private UdpSender() {
        udpInit();
    }

    public static UdpSender getInstance() {
        if(instance==null){
            synchronized (UdpSender.class){
                if(instance==null){
                    instance=new UdpSender();
                }
            }
        }
        return instance;
    }

    private void udpInit(){
        try{
            ds=new DatagramSocket(5566);
//            String ip = "192.168.0.9";
            Integer port=5684;
            String text="null";
            InetAddress inetAddress=InetAddress.getLocalHost();
            dp=new DatagramPacket(text.getBytes(),text.getBytes().length,inetAddress,port);

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("Udp对象创建成功");
        }
    }

    public void sendMsg(String msg){
        try{
            dp.setData(msg.getBytes());
            ds.send(dp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
